package com.lyhengtongwl.zqsnews.ui.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lyhengtongwl.putorefresh.BaseBothListener;
import com.lyhengtongwl.putorefresh.PullToBothLayout;
import com.lyhengtongwl.putorefresh.ViewStatus;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.adapter.NewsAdapter;
import com.lyhengtongwl.zqsnews.adapter.NewsPublicAdataper;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.entity.NewsBaseEntity;
import com.lyhengtongwl.zqsnews.entity.NewsEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.utils.NewsToastUtils;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

public class NewsSearchActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.pullRefresh)
    PullToBothLayout pullToBothLayout;
    @BindView(R.id.edit_)
    EditText edit_;
    private NewsPublicAdataper newsAdapter = null;
    private List<NewsEntity> list = new ArrayList<>();
    private int page = 1;//默认第一页
    private boolean isRefresh = true;//默认下拉刷新

    @Override
    public void setListen() {
        pullToBothLayout.setRefreshListener(new BaseBothListener() {
            @Override
            public void refresh() {
                isRefresh = true;
                page = 1;
                loadData();
            }

            @Override
            public void loadMore() {
                isRefresh = false;
                page++;
                loadData();
            }
        });
    }

    @Override
    public void setTitleBarColor() {
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @OnClick({R.id.title_left, R.id.tv_title_right})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.title_left://关掉页面
                finish();
                break;
            case R.id.tv_title_right://搜索
                if (edit_.getText().toString().length() >= 2) {
                    page=1;
                    isRefresh=true;
                    loadData();
                } else {
                    NewsToastUtils.showToast(App.getContext(), "搜索内容不合法！");
                }
                break;
        }
    }

    private void setAdapter() {
        if (newsAdapter == null) {
            newsAdapter = new NewsPublicAdataper(NewsSearchActivity.this, list, "2");
            recyclerView.setAdapter(newsAdapter);
            newsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    if (!"17".equals(list.get(position).getTypeId())) {
                        String imgUrl = list.get(position).getUrl();
                        String title = list.get(position).getTitle();
                        String articleReadId = list.get(position).getArticleId();
                        String articleId = list.get(position).getRandomCode();
                        String mDns = list.get(position).getmDns();
                        Bundle bundle = new Bundle();
                        bundle.putString("imgUrl", imgUrl);
                        bundle.putString("articleId", articleId);
                        bundle.putString("title", title);
                        bundle.putString("articleReadId", articleReadId);
//                        bundle.putString("dns", mDns);
                        NewsWebDetailActivity.forward(NewsSearchActivity.this, bundle);
                    }
                }
            });
        } else {
            newsAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_news_search;
    }

    @Override
    public void initView() {
        LinearLayoutManager manager = new LinearLayoutManager(NewsSearchActivity.this);
        recyclerView.setLayoutManager(manager);
    }

    @Override
    public void initData() {

    }
    //加载数据

    private void loadData() {
        String search = edit_.getText().toString();
        Task.getApiService().searchNewsArticle(search, page).enqueue(new MyCallback<NewsBaseEntity>(NewsSearchActivity.this) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                assert response.body() != null;
                if (("1").equals(response.body().getCode())) {
                    if (response.body().getData() != null) {
                        String jsonStr = JSONObject.toJSONString(response.body().getData());
                        JSONArray jsonArray = JSONArray.parseArray(jsonStr);
                        if (isRefresh) {
                            //表示下拉刷新
                            if (jsonArray.size() > 0) {
                                pullToBothLayout.showView(ViewStatus.CONTENT_STATUS);
                                list.clear();
                                for (int i = 0; i < jsonArray.size(); i++) {
                                    NewsEntity newsEntity = new NewsEntity();
                                    newsEntity.setMoney(jsonArray.getJSONObject(i).getInteger("incomeNum") + "");
                                    newsEntity.setTitle(jsonArray.getJSONObject(i).getString("title"));
                                    newsEntity.setUrl(jsonArray.getJSONObject(i).getString("image"));
                                    newsEntity.setArticleId(jsonArray.getJSONObject(i).getInteger("id") + "");
                                    newsEntity.setTypeId(jsonArray.getJSONObject(i).getInteger("typeId") + "");
                                    newsEntity.setmDns(jsonArray.getJSONObject(i).getString("remark"));
                                    newsEntity.setContentUrl(jsonArray.getJSONObject(i).getString("contentUrl"));
                                    newsEntity.setRandomCode(jsonArray.getJSONObject(i).getString("randomCode"));
                                    list.add(newsEntity);
                                }

//                        newsAdapter.notifyDataSetChanged();
                                setAdapter();
                            } else {
                                pullToBothLayout.showView(ViewStatus.EMPTY_STATUS);
                                ((TextView) pullToBothLayout.getView(ViewStatus.EMPTY_STATUS).findViewById(R.id.tv_content)).setText("没有搜索到文章");
                                pullToBothLayout.finishRefresh();
                                pullToBothLayout.finishLoadMore();
                                NewsToastUtils.showToast(App.getContext(), response.body().getMsg());
                            }
                            pullToBothLayout.finishRefresh();

                        } else {
                            if (jsonArray.size() > 0) {
                                //上拉加载
                                for (int i = 0; i < jsonArray.size(); i++) {
                                    NewsEntity newsEntity = new NewsEntity();
                                    newsEntity.setMoney(jsonArray.getJSONObject(i).getInteger("incomeNum") + "");
                                    newsEntity.setTitle(jsonArray.getJSONObject(i).getString("title"));
                                    newsEntity.setUrl(jsonArray.getJSONObject(i).getString("image"));
                                    newsEntity.setArticleId(jsonArray.getJSONObject(i).getInteger("id") + "");
                                    newsEntity.setTypeId(jsonArray.getJSONObject(i).getInteger("typeId") + "");
                                    newsEntity.setmDns(jsonArray.getJSONObject(i).getString("remark"));
//                            newsEntity.setContent(jsonArray.getJSONObject(i).getString("content"));
                                    newsEntity.setContentUrl(jsonArray.getJSONObject(i).getString("contentUrl"));
                                    newsEntity.setRandomCode(jsonArray.getJSONObject(i).getString("randomCode"));
                                    list.add(newsEntity);
                                }
//                        newsAdapter.notifyDataSetChanged();
                                setAdapter();
                            }
                            pullToBothLayout.finishLoadMore();
                        }

                    } else {
                        if (isRefresh) {
                            pullToBothLayout.showView(ViewStatus.EMPTY_STATUS);
                            ((TextView) pullToBothLayout.getView(ViewStatus.EMPTY_STATUS).findViewById(R.id.tv_content)).setText("没有搜索到文章");
                        }
                        pullToBothLayout.finishRefresh();
                        pullToBothLayout.finishLoadMore();
                        NewsToastUtils.showToast(App.getContext(), response.body().getMsg());
                    }
                } else {
                    if (isRefresh) {
                        pullToBothLayout.showView(ViewStatus.EMPTY_STATUS);
                        ((TextView) pullToBothLayout.getView(ViewStatus.EMPTY_STATUS).findViewById(R.id.tv_content)).setText("没有搜索到文章");
                    }
                    pullToBothLayout.finishRefresh();
                    pullToBothLayout.finishLoadMore();
                    NewsToastUtils.showToast(App.getContext(), response.body().getMsg());
                }


            }

            @Override
            protected void onFailure(Throwable t) {
                pullToBothLayout.finishRefresh();
                pullToBothLayout.finishLoadMore();
            }
        });
    }
}
